<?php

namespace App\Http\Controllers\Content;

use App\Http\Controllers\Controller;
use App\Http\Requests\ContentModelRequest;
use App\Models\ContentModel;
use Illuminate\Http\Request;

class ModelController extends Controller
{
    /**
     * Display a listing of the resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function index()
    {
//        从数据库中获取模型数据
        $models =  ContentModel::get();
//        渲染model模型的view视图模板
        return view('content.model_index',compact('models'));
    }

    /**
     * Show the form for creating a new resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function create()
    {
        //新增模型,渲染create新增模型的视图view
        return view('content.model_create');
    }

    /**
     * Show the form for editing the specified resource.
     *
     * @param  \App\Models\ContentModel  $contentModel
     * @return \Illuminate\Http\Response
     */
    public function edit(ContentModel $model)
//    public function edit(ContentModel $contentModel)
    {
//        dd($contentModel);
//        dd($model);
        //编辑模型渲染模板
//        dd($contentModel);die;
        return view('content.model_edit',compact('model'));
    }

    /**
     * Store a newly created resource in storage.
     *
     * @param  \Illuminate\Http\Request  $request
     * @return \Illuminate\Http\Response
     */
    public function store(ContentModelRequest $request)
    {
        //dd([$request->all(),$request->toArray()]);
        //通过上面的输出可以看出$request->all()转成的数组中fields对应的值是json格式的
        $data = $request->all();
        //将传递过来的json格式的数据,使用php中的函数decode成数组
        $data['fields'] = json_decode($data['fields'],true);
        //将处理过的数据保存到数据库中
        //通过相应模型的create方法将数组$data中的内容写入数据库
        ContentModel::create($data);
        //数据写入成功后重定向到首页,并通过with()方法往闪存中存入一条信息
        return redirect(route('content.model.index'))->with('success','模型创建成功');
    }

    /**
     * Display the specified resource.
     *
     * @param  \App\Models\ContentModel  $contentModel
     * @return \Illuminate\Http\Response
     */
    public function show(ContentModel $contentModel)
    {
        //
    }

    /**
     * Update the specified resource in storage.
     *
     * @param  \Illuminate\Http\Request  $request
     * @param  \App\Models\ContentModel  $contentModel
     * @return \Illuminate\Http\Response
     */
//    public function update(ContentModelRequest $request, ContentModel $contentModel)
    public function update(ContentModelRequest $request, ContentModel $model)
    {
        //
//        dd($contentModel);
        $data = $request->all();
        $data['fields'] = json_decode($data['fields'],true);
        $model->update($data);
        //更新成功后给用户提示
        return redirect(route('content.model.index'))->with('success','模型编辑更新成功');
    }

    /**
     * Remove the specified resource from storage.
     *
     * @param  \App\Models\ContentModel  $contentModel
     * @return \Illuminate\Http\Response
     */
    public function destroy(ContentModel $model)
    {
        //判断是不是系统模型
            if ($model['is_system'] == 1){
                return back()->with('error','系统模型不能删除');
            }
            $model->delete();
            //返回提示
        return redirect(route('content.model.index'))->with('success','删除成功');


    }
}
